package com.st.batch.listener;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        if(jobExecution.getStatus() == BatchStatus.COMPLETED) {

            //List<Person> results = jdbcTemplate.query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
            //    @Override
            //    public Person mapRow(ResultSet rs, int row) throws SQLException {
            //        return new Person(rs.getString(1), rs.getString(2));
            //    }
            //});
            //
            //for (Person person : results) {
            //    log.info("Found <" + person + "> in the database.");
            //}
            //jobExecution.setExitStatus(ExitStatus.STOPPED);
            //jobExecution.setStatus(BatchStatus.STOPPED);
        }
    }
}
